<?php

class LoginInfo{
	var $userInfo=false;
	var $sessionKey='LoginInfo';
	var $path = '';
	public function init()
	{
		$this->CI =& get_instance();
		$this->userInfo = $this->CI->session->userdata($this->sessionKey);
		$this->CI->LoginInfo = $this;

		if(!$this->userInfo){
			$this->SetGuest('Tamu');
		}
	
		/// EXCEPTION PATH
		$exception_path = array(
			'/',
			'session/login',
			'session/logout',
			'session/state'
		);


		$this->path = $this->CI->uri->segment(1).'/'.$this->CI->uri->segment(2);
		$this->landing_page = $this->path;//$_SERVER['REQUEST_URI'];
		//die($path);
		if(in_array($this->path, $exception_path))
		{

			return $this->AuthenticationPassed();	
		}
		else
		{
			if($this->userInfo['tipe_admin']=='Guest'){
				$this->AuthenticationFailed();
			}
			else{
				return $this->AuthenticationPassed();
			}
		}
		return $this->AuthenticationFailed();
	}

	public function SetGuest($name='Guest')
	{
		//echo 'SetGuest : called,br/>';
		$Login = array(
			'id'		=>'0',
			'name' 		=> $name,
			'tipe_admin' 	=> 'Guest',
			'keluar_terakhir' => date('Y-m-d H:i:s',time())
		);
		$this->SetUserInfo($Login);
	}
	public function GetUserInfo($key=0)
	{
		if($key)
			return $this->userInfo[$key];
		return $this->userInfo;
	}
	public function SetUserInfo($data)
	{
		$data['keluar_terakhir'] = date('Y-m-d H:i:s',time());
		$this->CI->session->set_userdata($this->sessionKey,$data);
		if($data['id'])
			$this->CI->db->where('id',$data['id'])->update('admin',array('keluar_terakhir'=>$data['keluar_terakhir']));
		$this->userInfo = $data;
	}
	public function UnsetUserInfo()
	{
		$this->userInfo = false;
		$this->CI->session->unset_userdata($this->sessionKey);
	}
	public function Redirect()
	{
		//print_r($_SERVER);
		redirect('session/login');
	}
	public function LandingPage()
	{
		# code...\
		//echo $this->landing_page;
		if($this->landing_page != 'session/login' )
			redirect($this->landing_page);
		else
			redirect('welcome/greeting');
	}
	public function AuthenticationPassed()
	{
		// $cls = $this->CI->uri->segment(1);
		// $fn  = $this->CI->uri->segment(2);
		
		//$this->landing_page = $path;//$_SERVER['REQUEST_URI'];
		//die($path);
		
		$config_acl = $this->CI->config->item('acl') ;
		
		$acl_cls 	= $this->CI->uri->segment(1);
		$acl_fn 	= $this->CI->uri->segment(2);
		
		// GET GID
		//$gid_list
		$gid = $config_acl['gid'][$this->userInfo['tipe_admin']]; 
		$acl_cls = empty($acl_cls) ? '/' : $acl_cls;
		if($acl_cls != '/')
		{
			$reg = $config_acl['registry'][$acl_cls][$acl_fn];
			if(!empty($reg))
			{
				if(in_array($gid, $reg))
				{
					//return
				}
				else
				{
					die('We are sorry but you dont have access to this resource.');
				}
			}
			else
			{
				die('Error : Access to unregistered modules...');
			}
		}
		
		//echo $this->GetUserInfo('tipe_admin');
		//die();
		//echo 'Congratulation You now can Access This Page<br/>';
	}
	public function AuthenticationFailed()
	{
		// echo 'Sorry You Have To Login To Access This Page<br/>';
		// sleep(2);
		$this->Redirect();
	}
	public function IsLoggedIn()
	{
		return $this->GetUserInfo('tipe_admin') != 'Guest';
	}
	public function IsAdmin()
	{
		return $this->GetUserInfo('tipe_admin') == 'Admin';
	}
	public function Is($group)
	{
		// $ret = FALSE;
		// if(is_array($group))
		// {
		// 	foreach ($group as $grp) {
		// 		$ret = $ret && $this->GetUserInfo('tipe_admin') == $group;
		// 	}
		// 	return $ret;
		// }
		return $this->GetUserInfo('tipe_admin') == $group;
	}
} 